#include "DataManager.h"
#include "Define.h"
#include "DataManagerProxy.h"
#include "DataSubscribe.h"
#include "DataConsumer.h"
#include <QDebug>
DataManager::DataManager() {

}

// void DataManager::OnData(std::string key, QVariant val)
// {
//     //consumerMap[key]->OnData(key, val);
// }

void DataManager::Startup()
{
    Config config;
    DataManagerProxy dmp;
    auto lstModules = dmp.loadModuleInfos(config.serverId, config.appName);

    for(auto itr = lstModules.consumers.begin(); itr != lstModules.consumers.end(); ++itr){
        //ScopedPointer<DataConsumer> consumer {new DataConsumer()};
        DataConsumer * consumer = new DataConsumer();
        ConsumerInfo ci = *itr;
        consumer->Setup(ci);
        consumerMap.insert(QString(itr->SubscribeName.c_str()),consumer);
    }

    for(auto itr = lstModules.modules.begin();itr!= lstModules.modules.end();++itr)
    {
        ModuleInfo mi = *itr;

        if(consumerMap.contains(itr->ClassName.c_str())){
            //QScopedPointer<DataSubscribe> subscriber(new DataSubscribe());
            DataSubscribe* subscriber = new DataSubscribe();
            subscriber->regConsumer(consumerMap[itr->ClassName.c_str()]);
            subscriber->Setup(mi);
            //subscriber->Run();
            subscribers.append(subscriber);
        }
    }

}
